package cn.lg.soar.system.biz.modules.general.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.general.entity.UrlMapping;
import cn.lg.soar.system.biz.modules.general.mapper.UrlMappingMapper;
import cn.lg.soar.system.biz.modules.general.service.UrlMappingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 地址映射
 * @author luguoxiang
 * @since 2025-05-10
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class UrlMappingServiceImpl extends BaseServiceImpl<UrlMappingMapper, UrlMapping> implements UrlMappingService {


    @Cacheable(value = SystemCacheKey.URL_MAPPING, key = "#p0")
    @Override
    public Map<String, String> getByGroup(String group) {
        List<UrlMapping> list = list(
                Wrappers.<UrlMapping>lambdaQuery()
                        .select(UrlMapping::getBizType, UrlMapping::getUrl)
                        .eq(UrlMapping::getGroupCode, group)
        );
        AssertUtil.notEmpty(list, "分组不存在请检查");
        return list.stream().collect(Collectors.toMap(UrlMapping::getBizType, UrlMapping::getUrl));
    }

    @Override
    public Map<String, Map<String, String>> getMapping() {
        Map<String, List<UrlMapping>> group = list(
                Wrappers.<UrlMapping>lambdaQuery()
                        .select(UrlMapping::getGroupCode, UrlMapping::getBizType, UrlMapping::getUrl)
        ).stream().collect(Collectors.groupingBy(UrlMapping::getGroupCode));

        Map<String, Map<String, String>> res = new HashMap<>(group.size());
        group.forEach((key, value) -> {
            Map<String, String> map = value.stream().collect(Collectors.toMap(UrlMapping::getBizType, UrlMapping::getUrl));
            res.put(key, map);
            cacheManager.getCache(SystemCacheKey.URL_MAPPING).put(key, map);
        });
        return res;
    }

    @CacheEvict(value = SystemCacheKey.URL_MAPPING, key = "#p0.groupCode")
    @Override
    public boolean save(UrlMapping entity) {
        return super.save(entity);
    }

    @CacheEvict(value = SystemCacheKey.URL_MAPPING, key = "#p0.groupCode")
    @Override
    public boolean updateById(UrlMapping entity) {
        return super.updateById(entity);
    }

    @CacheEvict(value = SystemCacheKey.URL_MAPPING, allEntries = true)
    @Override
    public boolean removeByIds(Collection<?> list) {
        return super.removeByIds(list);
    }

    @CacheEvict(value = { SystemCacheKey.URL_MAPPING }, allEntries = true)
    @Override
    public void clearCache() {}

    @Override
    public List<String> getGroups() {
        return listObjs(
                Wrappers.<UrlMapping>lambdaQuery()
                        .select(UrlMapping::getGroupCode)
                        .groupBy(UrlMapping::getGroupCode),
                Object::toString
        );
    }

    @Override
    public List<String> getBizTypes() {
        return listObjs(
                Wrappers.<UrlMapping>lambdaQuery()
                        .select(UrlMapping::getBizType)
                        .groupBy(UrlMapping::getBizType),
                Object::toString
        );
    }

}
